package etm.servlet.pagocuota;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import upc.condominio.modelo.Cuota;
import etm.excepcion.DAOExcepcion;
import etm.negocio.GestionPagoCuota;
import etm.negocio.GestionRegCuotas;


@WebServlet("/PagoCuotaInsertarServlet")
public class PagoCuotaInsertarServlet extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public PagoCuotaInsertarServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 * HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("PagoCuotaInsertarServlet");
		this.doPost(request, response);
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 * HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {		
		
		try {
			System.out.println("idCuota ==>"+ request.getParameter("idCuota"));
			int idCuota = Integer.parseInt(request.getParameter("idCuota"));
			GestionRegCuotas negCuota=new GestionRegCuotas();
			Cuota cuota=negCuota.obtener(idCuota);
			String tipoPago = request.getParameter("tipoPago");
			double importe = Double.parseDouble(request.getParameter("importe"));
			java.util.Date utilDate = new java.util.Date();	    
			Date fechaPago = new java.sql.Date(utilDate.getTime());			
			
			GestionPagoCuota negPago = new GestionPagoCuota();		
			negPago.insertar(cuota,tipoPago,fechaPago,importe);
			negCuota.actualizarSaldo(idCuota,importe,-1);
			System.out.println("redirecionar a RegistroCuotas ==>");
			response.sendRedirect(request.getContextPath()	+ "/RegistroCuotas.jsp");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}

	}
}